﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework.Utils;
using CNVP.Framework;

namespace CNVP.JShop.Web.Member
{
    public partial class ReSetPassword : System.Web.UI.Page
    {
        private Guid GuID_id;
        Model.CNVP_RetrievePwdInfo model_RetrievePwdInfo = new Model.CNVP_RetrievePwdInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Convert.ToString(Request.QueryString["ID"])) && IsGuid(Convert.ToString(Request.QueryString["ID"])))
            {
                GuID_id = new Guid(Convert.ToString(Request.QueryString["ID"]));
            }
            else
            {
                MessageUtils.ShowPre("悲剧，出错了！");
            }
            if (!IsPostBack)
            {
                try
                {
                    model_RetrievePwdInfo = new BLL.CNVP_RetrievePwdInfo().GetSingle_CNVP_RetrievePwdInfoById(GuID_id);
                    if (model_RetrievePwdInfo != null)
                    {
                        DateTime nowTime = DateTime.Now;
                        DateTime postTime = model_RetrievePwdInfo.PostTime;
                        TimeSpan npSpan = nowTime - postTime;
                        if (npSpan.Days >= 1)
                        {
                            BasePage.MessageBox("抱歉，您的链接失效。您可以重新申请找回密码或直接登录！", "../Index.aspx");
                            return;
                        }

                    }
                    else
                    {
                        MessageUtils.ShowPre("链接不存在或已经失效。您可以重新申请找回密码或直接登录。");
                    }
                }
                catch
                {
                    MessageUtils.ShowPre("链接不存在或已经失效。您可以重新申请找回密码或直接登录。");
                }
                #region js验证
                txtPwd1.Attributes.Add("onFocus", "RegTipInfo('lbPwd1','请输入密码．')");
                txtPwd1.Attributes.Add("onBlur", "return RegCheck('txtPwd1','lbPwd1')");
                txtPwd2.Attributes.Add("onFocus", "RegTipInfo('lbPwd2','请输入确认密码，与上面一致．')");
                txtPwd2.Attributes.Add("onBlur", "return RegCheck('txtPwd2','lbPwd2')");
                btUp.Attributes.Add("onClick", "return ReSetPassword()");
                #endregion
            }
        }


        public static bool IsGuid(string value)
        {
            try
            {
                Guid myguid = new Guid(value);
                return true;
            }
            catch
            {
                return false;
            }
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            try
            {
                string UserPass1 = txtPwd1.Text.Trim();
                string UserPass2 = txtPwd2.Text.Trim();
                if ((UserPass1.Length < 6 || UserPass1.Length > 20) || string.IsNullOrEmpty(UserPass1))
                {
                    MessageUtils.ShowPre("新密码不能为空或格式不正确！");
                    return;
                }
                if (UserPass2.Length < 6 || UserPass2.Length > 20 || string.IsNullOrEmpty(UserPass2))
                {
                    MessageUtils.ShowPre("确认密码不能为空或格式不正确！");
                    return;
                }
                if (UserPass2 != UserPass1)
                {
                    MessageUtils.ShowPre("新密码和确认密码不相等！");
                    return;
                }
                if (Session["CheckCode"] != null)
                {
                    if (Session["CheckCode"].ToString() == Request.Form["txtVerifyCode"].Trim().ToLower())
                    {
                        model_RetrievePwdInfo = new BLL.CNVP_RetrievePwdInfo().GetSingle_CNVP_RetrievePwdInfoById(GuID_id);
                        if (model_RetrievePwdInfo != null)
                        {
                            DateTime nowTime = DateTime.Now;
                            DateTime postTime = model_RetrievePwdInfo.PostTime;
                            TimeSpan npSpan = nowTime - postTime;
                            if (npSpan.Days >= 1)
                            {
                                BasePage.MessageBox("抱歉，您的链接失效。您可以重新申请找回密码或直接登录！", "../Index.aspx");
                                return;
                            }
                            else
                            {
                                new BLL.CNVP_Users().UpdatePwd(EncryptUtils.MD5(UserPass1), model_RetrievePwdInfo.UserName);
                                new BLL.CNVP_RetrievePwdInfo().DeleteObject(GuID_id);
                                MessageUtils.ShowRedirect("密码修改成功！", "Login.aspx");
                            }
                           
                        }
                    }
                    else
                    {
                        BasePage.MessageBoxGoHistory("输入验证码有误！");
                    }
                }
            }
            catch
            {
                MessageUtils.ShowPre("悲剧，出错了！");
            }
        }
    }
}